package com.sc.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.sc.bean.Shu;
import com.sc.dao.ShuDao;


@Controller
public class ShuController {
	
	@Autowired
	private ShuDao shuDao;

	@RequestMapping("addshu")
	public String addshu(Model model,HttpServletRequest request,@RequestParam(value="photo") MultipartFile photo){
		String realPath=request.getServletContext().getRealPath("/images");
		System.out.println(request.getParameter("photo"));
		String uploadPath=realPath+"/"+photo.getOriginalFilename();
		System.out.println("文件名字="+photo.getOriginalFilename());
		System.out.println("uploadPath="+uploadPath);
		
		try {
			IOUtils.copy(photo.getInputStream(),new FileOutputStream(new File(uploadPath)));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String str="";
		Shu shu=new Shu();
		shu.setS_address(request.getParameter("address"));
		shu.setS_img(photo.getOriginalFilename());
		shu.setS_name(request.getParameter("name"));
		int i = shuDao.addShu(shu);
		if(i!=1){
			str="添加失败";
		}else{
			str="添加成功";
		}
		model.addAttribute("str", str);
		return"shu";
	}
	
	@RequestMapping("queryShu")
	@ResponseBody
	public Shu queryShu(int s_id){
		
		Shu shu=shuDao.queryShu(s_id);
		System.out.println(shu.getS_name());
		return shu;
		
	}
	
	@RequestMapping("test")
	public String test(Model model,String date){		
		model.addAttribute("date", date);
		return "shu";
	}
	
}
